package cycling.taxi.com.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.R;
import cycling.taxi.com.entity.Repair;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

public class DispathActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
    private List<Repair> data;
    private RequestQueue queue;
    private ShopInfoAdapter adapter;
    @InjectView(R.id.dispatch_lv)
    ListView dispatch_lv;
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.title_text)
    TextView title_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dispath);
        ButterKnife.inject(this);
        InitView();
        volleyPost();
    }
    private void InitView() {
        back=(ImageView)findViewById(R.id.back);
        title_text=(TextView)findViewById(R.id.title_text);
        back.setOnClickListener(this);
        title_text.setText("消息中心");
        adapter=new ShopInfoAdapter();
        queue= Volley.newRequestQueue(this);
        dispatch_lv.setOnItemClickListener(this);


    }
    //日期格式化
    public static String getStringDate(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }
    public void volleyPost(){
        // String path= "http://192.168.1.100:8080/CyclingBgTV1/appNotice/notice.dop";
        String path = Urls.getNotice();
        StringRequest request = new StringRequest(Request.Method.POST,path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                data = new Gson().fromJson(s,new TypeToken<List<Repair>>(){}.getType());
                LogUtil.e(s);
            //    pb.setVisibility(View.GONE);
                dispatch_lv.setAdapter(adapter);

            }
        },null){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String > map= new HashMap<String,String>();
                String beanJson= SPUtil.getInstance().getData("beanJson");
                //   String phone = SPUtil.getInstance().getData("phone");
                map.put("beanJson",beanJson);
                //   map.put("phone",phone);

                return map;
            }
        };
        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.back){
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //ToastUtil.getInstance().toast("111");
        Intent dispathItem= new Intent(this, DispathItemActivity.class);
        Adapter adpter=parent.getAdapter();
        Repair item=(Repair) adpter.getItem(position);//拿到当前数据值并强转   adpter.getItem(i)即为当前数据对象
        dispathItem.putExtra("Dispath", item);
        dispathItem.setClass(this,DispathItemActivity.class);
        startActivity(dispathItem);
    }


    class ShopInfoAdapter extends BaseAdapter {

        public ShopInfoAdapter(){

        }
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=View.inflate(DispathActivity.this,R.layout.activity_notice_item,null);

            }
            Repair repair = data.get(position);
            TextView nameTV =(TextView)convertView.findViewById(R.id.tv_item_name);
         //   TextView priceTV = (TextView)convertView.findViewById(R.id.tv_item_price);
            TextView tv_item_status=(TextView)convertView.findViewById(R.id.tv_item_status);
         //   TextView tv_item_createtime=(TextView)convertView.findViewById(R.id.tv_item_createtime);
          //  TextView tv_item_finshtime=(TextView)convertView.findViewById(R.id.tv_item_finshtime);
            TextView tv_item_dispatch=(TextView)convertView.findViewById(R.id.tv_item_dispatch);
         //   TextView tv_item_problem=(TextView)convertView.findViewById(R.id.tv_item_problem);
            if(repair.getUserTel()!=null) {
                nameTV.setText(repair.getUserTel());
            }
//            if(repair.getImie()!=null){
//                priceTV.setText(repair.getImie());
//            }
            if(repair.getState()!=null) {
                if(repair.getState().equals("0")) {
                    tv_item_status.setText("未完成");
                }else {
                    tv_item_status.setText("已完成");
                }
            }
//            if(repair.getFinishTime()!=null) {
//                tv_item_createtime.setText(getStringDate(repair.getCreateTime()));
//            }
//            if(repair.getFinishTime()!=null){
//                tv_item_finshtime.setText(getStringDate(repair.getFinishTime()));
//            }
            if(repair.getDispatchAddress()!=null) {
                tv_item_dispatch.setText(repair.getName());
            }
//            if(repair.getProblemDescription()!=null) {
//                tv_item_problem.setText(repair.getProblemDescription());
//            }
            return convertView;
        }
    }
}
